﻿namespace BEGDC.CC.Web.UserControls.EBD
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Collections;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using BEGDC.CC.BL.CCFlow.EBD;
    using BEGDC.CC.BL.CCFlow.CV;
    using BEGDC.CC.Entity;
    using BEGDC.CC.BL.Common.Query;
    using BEGDC.CC.BL.Workflow;
    using BEGDC.CC.BL.Common;
    using BEGDC.Utility.Castle;
    using BEGDC.CC.BL.UserManagement;
    using BEGDC.Utility.ExceptionManagement;

    /// <summary>
    ///		Summary description for NormalRequest.
    /// </summary>
    public class EBDRequest : BaseEBD
    {
        #region Variables
        protected BEGDC.CC.Web.UserControls.EBD.EBDCtrl EBDCtrl;
        protected BEGDC.CC.Web.UserControls.Export ctrlExport;
        protected System.Web.UI.WebControls.Button Btn_Submit;
        protected System.Web.UI.WebControls.Button Btn_Save;
        protected System.Web.UI.WebControls.Button Btn_Reply;
        protected System.Web.UI.WebControls.TextBox tbOperationFlag1;
        protected System.Web.UI.WebControls.Button Btn_Copy;
        protected System.Web.UI.WebControls.Label lbAppNo;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_title;
        protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenScrollHight;
        protected System.Web.UI.WebControls.TextBox Ctl_hiddenDept;
        protected System.Web.UI.WebControls.TextBox Ctl_hiddenBranch;
        protected System.Web.UI.WebControls.TextBox tbComment;
        protected System.Web.UI.WebControls.TextBox tbOperationFlag;
        protected System.Web.UI.HtmlControls.HtmlGenericControl SampleEZ;
        protected System.Web.UI.HtmlControls.HtmlGenericControl SampleNormal;
        protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;
        //protected System.Web.UI.WebControls.Button btnClose;
        protected BEGDC.CC.Web.Common.StampCtrl StampCtrl;
        protected bool _Transition = false;
        public bool isReadOnly = false;
        bool saveBeforeSubmit = false;//joan 2008/8/23
        #endregion

        #region Properties
        private bool IsQuery
        {
            get
            {
                string state = this.Request.QueryString["State"];
                if (state == null) return false;
                return state == SysDicItem.ProjectStatusFlag.QUERY;
            }
        }

        private bool IsView
        {
            get
            {
                string state = this.Request.QueryString["State"];
                if (state == null) return false;
                if (state == SysDicItem.ProjectStatusFlag.View || state == SysDicItem.ProjectStatusFlag.CANCEL
                    || state == SysDicItem.ProjectStatusFlag.Pending || state == SysDicItem.ProjectStatusFlag.End
                    || state == SysDicItem.ProjectStatusFlag.Participation)
                    return true;
                else
                    return false;
            }
        }
        protected string Step
        {
            get
            {
                IQuery queryService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
                FlowInstance flowInstance = queryService.QueryFlowInstance(this.AppID);
                if (flowInstance == null)
                    return WorkTransition.Draft;
                else
                    return flowInstance.CurrentActiveStep.StepCode;
            }
        }
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region Invalid Session
                Session[SessionFlag.AccompanySession] = null;
                Session[SessionFlag.CustomerSession] = null;
                Session[SessionFlag.AccompanySessionEZ] = null;
                Session[SessionFlag.CustomerSessionEZ] = null;

                Session[SessionFlag.CompanyCarSession] = null;
                Session[SessionFlag.CompanyCarSessionEZ] = null;
                Session[SessionFlag.LeaseCarSession] = null;
                Session[SessionFlag.LeaseCarSessionEZ] = null;
                Session[SessionFlag.HotelSession] = null;
                Session[SessionFlag.HotelSessionEZ] = null;

                Session[SessionFlag.ContractInfo] = null;
                Session[SessionFlag.CostCenter] = null;
                #endregion

                base.PreInitForm();
                BuildFormProperties();

                //this.Btn_Copy.Attributes["onclick"] = "javascript:OnCopy();";
                //this.Btn_Submit.Attributes["onclick"] = "javascript:OpenAssignmentPane("+this.AppID+",'"+this.Transition+"');";
                this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
                this.Btn_Reply.Attributes.Add("onclick", "javascript:OpenReplyPane(" + Request.QueryString["FlowID"] + ");");
                //this.btnClose.Attributes.Add("onclick", "javascript:SimpleCloseWindow();");

                InitForm();

                if (this.IsView)
                {
                    this.TR_title.Visible = false;
                }
            }
            ButtonStatusSetting();
            //			Page.SmartNavigation = true;
        }

        #region ControlStatusSetting
        private void ControlStatusSetting(Accessibility access)
        {
            if (access == Accessibility.Readonly)
            {
                this.DisableChildrenControls(this);
                //this.stampCtrl1.Visible = false;
            }
        }
        #endregion

        #region Form Properties
        protected bool Transition
        {
            get
            {
                string TranStr = (string)this.ViewState["Transition"];
                if (TranStr == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                if (value)
                {
                    this.ViewState["Transition"] = "1";
                }
                else
                {
                    this.ViewState["Transition"] = "0";
                }
            }
        }
        #endregion

        #region Build Form Properties
        private void BuildFormProperties()
        {
            IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
            Employee CurrentRequestor = userService.GetUser(HttpContext.Current.User.Identity.Name);

            if (CurrentRequestor.Department != null)
            {
                //				this.Branch = CurrentRequestor.Department.Id.ToString();
                //				this.Ctl_hiddenBranch.Text = this.Branch;
                //				this.Department = CurrentRequestor.Department.DepartmentType.DicItemKey;
                //				this.Ctl_hiddenDept.Text = this.Department;
            }

            string TranStr = Request.QueryString["Transition"];

            if (TranStr != null && TranStr != "")
            {
                if (TranStr == "1")
                {
                    this.Transition = true;
                }
                else
                {
                    this.Transition = false;
                }
            }
        }
        #endregion

        #region InitForm
        private void InitForm()
        {
            long flowId = this.LoadApplication();

            if (this.Application != null)
            {
                if (this.Application.AppNo != string.Empty)
                    this.lbAppNo.Text = "Application No:" + this.Application.AppNo;
                //UPDATE FORM PROPERTIES IF IT IS QUERY BACK
                //				if(this.Application.DepartmentType!=null)
                //				{
                //					this.Department = this.Application.DepartmentType.DicItemKey;
                //				}
                //				if(this.Application.DepartmentName!=null)
                //				{
                //					this.Branch = this.Application.DepartmentName.Id.ToString();
                //				}
                this.Transition = this.Application.IsTransition;

                //Display Copy Button
                if (flowId == 0)
                {
                    //this.Btn_Copy.Visible = true;
                    this.StampCtrl.Visible = false;
                }

                //SET PROPERTIES OF FORM CONTROL AND LOAD CTRL
                EBDCtrl.isCoveredPerson = !this.Transition;
                EBDCtrl.isReadOnly = isReadOnly;

                this.SampleNormal.Visible = !this.Transition;
                this.SampleEZ.Visible = this.Transition;

            }
            else if (this.AppID == 0 && this.Application == null)
            {
                this.StampCtrl.Visible = false;
                //SET PROPERTIES OF FORM CONTROL AND LOAD CTRL
                EBDCtrl.isCoveredPerson = !this.Transition;
                EBDCtrl.isReadOnly = isReadOnly;

                this.SampleNormal.Visible = !this.Transition;
                this.SampleEZ.Visible = this.Transition;
            }
            else
            {
                WebUtility.Go2ErrorPage(this.Page, "wrong Application ID");
            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Reply.Click += new EventHandler(Btn_Reply_Click);
            this.Btn_Save.Click += new EventHandler(Btn_Save_Click);
            this.Btn_Submit.Click += new EventHandler(Btn_Submit_Click);
            //this.Btn_Copy.Click += new EventHandler(Btn_Copy_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region BuildObjects

        private DataTable BuildSessionTable(string SessionName)
        {
            DataTable dataTable = null;
            object SessDataTable = Session[SessionName];
            if (SessDataTable != null)
            {
                dataTable = (DataTable)SessDataTable;
            }
            return dataTable;
        }
        #endregion

        #region Save
        void Btn_Save_Click(object sender, EventArgs e)
        {
            string url = Request.Form["tbURL"];
            string script = "<script>";
            try
            {
                //OBTAIN BASIC INFORMATION
                IEBD EBDService = AppContainer.Instance[ServiceConst.EBDService] as IEBD;
                IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                //BEGDC.CC.Entity.Application App = EBDService.GetApplication(this.AppID);

                Application app = new Application();
                app.IsTransition = this.Transition;
                if (this.Application != null)
                {
                    app.Id = this.AppID;
                    app.AppNo = this.Application.AppNo;
                    app.IsTransition = this.Application.IsTransition;
                    app.NeedEVPApproval = this.Application.NeedEVPApproval;
                    //app.DepartmentType = this.Application.DepartmentType;
                    //app.DepartmentName = this.Application.DepartmentName;
                }

                CustomerInfo customer = new CustomerInfo();
                BusinessPurpose business = new BusinessPurpose();
                Topic topic = new Topic();
                SalesTeamInfo salesTeam = new SalesTeamInfo();
                LogisticalInfo logist = new LogisticalInfo();
                PaymentInfo payment = new PaymentInfo();
                IList customerAttendee = new ArrayList();
                IList alAttendee = new ArrayList();
                IList site = new ArrayList();
                IList cost = new ArrayList();
                IList contract = new ArrayList();
                //update Jovi
                DataTable HotelTable = null;
                DataTable CompanyCarTable = null;
                DataTable LeaseCarTable = null;

                EBDCtrl.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract, ref HotelTable, ref CompanyCarTable, ref LeaseCarTable);
                app.ApplicationType = WorkflowTypes.EBD;
                EBDService.SaveEZApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost, contract, HotelTable, CompanyCarTable, LeaseCarTable, false, true);

                this.AppID = app.Id;
                this.Application = app;
                EBDCtrl.CustomerID = customer.Id;
                EBDCtrl.TopicID = topic.Id;
                EBDCtrl.SalesTeamID = salesTeam.Id;
                EBDCtrl.LogistID = logist.Id;
                EBDCtrl.BusinessID = business.Id;
                EBDCtrl.PaymentID = payment.Id;

                Session["AppID"] = this.Application.Id;

                if (url == string.Empty)
                    script += "VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');";
                if (url != string.Empty)
                {
                    if (url.IndexOf("AppID") == -1)
                        url = url.Substring(0, url.IndexOf("?") + 1) + "AppID=" + app.Id + "&" + url.Substring(url.IndexOf("?") + 1);
                    script += "window.location.href='" + url + "'";
                }
                else
                {
                    url = Request.Url.AbsoluteUri;
                    if (url.IndexOf("AppID") == -1)
                        url = url.Substring(0, url.IndexOf("?") + 1) + "AppID=" + app.Id + "&" + url.Substring(url.IndexOf("?") + 1);
                    script += "window.location.href='" + url + "'";
                }
                script += "</script>";

                //Page.RegisterClientScriptBlock("",script);

                //this.Btn_Copy.Visible = true;
                this.ctrlExport.Visible = true;

            }
            catch (Exception exp)
            {
                throw exp;
            }
            //<--joan 2008/8/23
            if (!this.saveBeforeSubmit)
            {
                Page.RegisterClientScriptBlock("", script);
            }

            //-->end 
        }
        #endregion

        #region Submit
        void Btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                #region Check Attachment
                string msg;
                this.saveBeforeSubmit = true;
                this.Btn_Save_Click(this.Btn_Save, null);
                this.saveBeforeSubmit = false;

                msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage(this.AppID);

                //				IList attTypes = new ArrayList();
                //				attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.AGENDA);
                //
                //				if(this.EBDCtrl.isNeedGift)
                //					attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.GIFT);
                //				if(this.EBDCtrl.isNeedHotel)
                //					attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.HOTEL);
                //				if(this.EBDCtrl.isNeedVehicle)
                //					attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.VEHICLE);
                //
                //				if((this.Application==null && this.Transition == false) || (this.Application!=null && this.Application.IsTransition == false))
                //				{
                //					attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.BUDGET);
                //					if(this.EBDCtrl.isNeedVisa)
                //						attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.VISA);
                //				}
                //
                //				IUtility utiService = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                //				CheckBoxList cbl = this.EBDCtrl.VisitingSiteControl;
                //				for(int i=0;i<cbl.Items.Count;i++)
                //				{
                //					ListItem item = cbl.Items[i] as ListItem;
                //					if(item.Selected)
                //					{
                //						DicItem dicitem = utiService.GetDicItem(Convert.ToInt32(item.Value));
                //						if(dicitem!=null)
                //						{
                //							if(dicitem.Description.ToUpper()=="Others".ToUpper())
                //							{
                //								//Check the Others Location Comment
                //								if(this.EBDCtrl.OtherSiteLocation == string.Empty || this.EBDCtrl.OtherSiteLocation == "")
                //								{
                //									Page.RegisterClientScriptBlock("","<script>alert('Other Site Should be specified.');</script>");
                //									return;
                //								}
                //							}
                //							if(dicitem.Description.ToUpper()=="OCO-Shanghai Factory".ToUpper())
                //							{
                //								attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.ASBVISIT);
                //							}
                //							if(dicitem.Description.ToUpper()=="OCO-Shanghai Demo Center".ToUpper())
                //							{
                //								attTypes.Add(BEGDC.CC.BL.CCFlow.AttachmentType.DEMOCENTER);
                //							}
                //						}
                //					}
                //				}
                //				
                //				msg = BEGDC.CC.BL.CCFlow.AttachmentService.GetMissedAttachmentMessage( this.AppID, attTypes );

                if (msg != "" && msg != string.Empty)
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('" + msg + "')</script>");
                    return;
                }
                #endregion

                #region Check Someone Else
                //				if(this.EBDCtrl.IsSomeoneElse&&this.EBDCtrl.SomeoneEmployee==null)
                //				{
                //					Page.RegisterClientScriptBlock("","<script>alert('Please specify some one else you are delegating for');</script>");
                //					return;
                //				}
                #endregion

                #region Check Other location
                IUtility utiService = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                CheckBoxList cbl = this.EBDCtrl.VisitingSiteControl;
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    ListItem item = cbl.Items[i] as ListItem;
                    if (item.Selected)
                    {
                        DicItem dicitem = utiService.GetDicItem(Convert.ToInt32(item.Value));
                        if (dicitem != null)
                        {
                            if (dicitem.Description.ToUpper() == "Others".ToUpper())
                            {
                                //Check the Others Location Comment
                                if (this.EBDCtrl.OtherSiteLocation == string.Empty || this.EBDCtrl.OtherSiteLocation == "")
                                {
                                    Page.RegisterClientScriptBlock("", "<script>alert('Other Site Should be specified.');</script>");
                                    return;
                                }
                            }
                        }
                    }
                }
                #endregion

                Implement_Submit();
            }
            catch (GDCException ex)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + ex.Message + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
            }
        }

        private Employee GetUser(string name)
        {
            IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
            return userService.GetUser(name);
        }

        private void Implement_Submit()
        {
            try
            {
                //OBTAIN BASIC INFORMATION
                IEBD EBDService = AppContainer.Instance[ServiceConst.EBDService] as IEBD;
                IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                BEGDC.CC.Entity.Application App = EBDService.GetApplication(this.AppID);

                //if(this.EBDCtrl.IsSomeoneElse&&this.EBDCtrl.SomeoneEmployee!=null)
                //{
                //BEGDC.CC.Entity.Employee Someone = this.EBDCtrl.SomeoneEmployee;
                //this.Branch = Someone.Department.Id.ToString();
                //this.Department = Someone.Department.DepartmentType.DicItemKey;
                //}

                Application app = new Application();
                app.IsTransition = this.Transition;
                if (this.Application != null)
                {
                    app.Id = this.AppID;
                    app.AppNo = this.Application.AppNo;
                    if (App.AppNo.IndexOf("-D") > -1)
                        App.AppNo = string.Empty;
                    app.IsTransition = this.Application.IsTransition;
                    app.NeedEVPApproval = this.Application.NeedEVPApproval;
                    app.DepartmentType = this.Application.DepartmentType;
                    app.DepartmentName = this.Application.DepartmentName;
                }

                CustomerInfo customer = new CustomerInfo();
                BusinessPurpose business = new BusinessPurpose();
                Topic topic = new Topic();
                SalesTeamInfo salesTeam = new SalesTeamInfo();
                LogisticalInfo logist = new LogisticalInfo();
                PaymentInfo payment = new PaymentInfo();
                IList customerAttendee = new ArrayList();
                IList alAttendee = new ArrayList();
                IList site = new ArrayList();
                IList cost = new ArrayList();
                IList contract = new ArrayList();

                DataTable HotelTable = null;
                DataTable CompanyCarTable = null;
                DataTable LeaseCarTable = null;

                EBDCtrl.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract, ref HotelTable, ref CompanyCarTable, ref LeaseCarTable);
                app.ApplicationType = WorkflowTypes.EBD;

                if (!CheckFormValidation(app))
                {
                    return;
                }

                EBDService.SubmitEZApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost, contract, HotelTable, CompanyCarTable, LeaseCarTable, string.Empty, true);

                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
            }
        }

        private void BindAppDepartment(ref BEGDC.CC.Entity.Application app)
        {
            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            app.IsTransition = this.Transition;
            //			if(app.SomeoneElseNo!=null)
            //			{
            //				app.DepartmentType = utility.GetDicItem(app.SomeoneElseNo.Department.DepartmentType.DicItemKey,  BEGDC.CC.BL.Common.DicType.DepartmentType);
            //				
            //				app.DepartmentName = new Department();
            //				app.DepartmentName.Id = Convert.ToInt32(app.SomeoneElseNo.Department.Id);
            //			}
            //			else
            //			{
            //				if(app.DepartmentType==null&&this.Department!="")
            //				{
            //					app.DepartmentType = utility.GetDicItem(this.Department,  BEGDC.CC.BL.Common.DicType.DepartmentType);
            //				}
            //				if(app.DepartmentName==null&&this.Branch!=null&&this.Branch!="")
            //				{
            //					app.DepartmentName = new Department();
            //					app.DepartmentName.Id = Convert.ToInt32(this.Branch);
            //				}
            //			}
        }

        private bool CheckFormValidation(Entity.Application App)
        {
            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            IEBD EBDService = AppContainer.Instance[ServiceConst.EBDService] as IEBD;

            if (this.EBDCtrl.CustomerNum == 0)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('At least one customer should be specified.');</script>");
                return false;
            }
            if (this.EBDCtrl.AccompanyNum == 0)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('At least one Alcatel-Lucent attendee should be specified.');</script>");
                return false;
            }
            if (this.EBDCtrl.CostCenterNum == 0)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('At least one cost center code should be specified.');</script>");
                return false;
            }
            if (this.EBDCtrl.IsCustomerNotInCIS)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('There are customer(s) not in CIS.');</script>");
                return false;
            }

            //			Employee emp = this.GetUser(HttpContext.Current.User.Identity.Name);
            //			if(this.EBDCtrl.IsSomeoneElse&&this.EBDCtrl.SomeoneEmployee!=null)
            //			{
            //				emp = this.EBDCtrl.SomeoneEmployee;
            //			}

            string Empl = null;
            if (App.IsSSME)
            {
                Empl = EBDService.GetCostCenterHead(BEGDC.CC.BL.Workflow.UserRole.CostCenterHead, App.CostCenterCode.DicItemKey);
                if (Empl == "" || Empl == "0")
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('No Cost Center Head available.');</script>");
                    return false;
                }
            }
            else
            {
                Empl = EBDService.GetOCORoleMemberList(BEGDC.CC.BL.Workflow.UserRole.DeptHead, App.DepartmentType.DicItemKey);
                if (Empl == "" || Empl == "0")
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('No Department Head available.');</script>");
                    return false;
                }

                DataTable table = utility.GetDepartmentFNCByCode(App.DepartmentType.DicItemKey);
                if (table == null || table.Rows.Count == 0)
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('No FNC available.');</script>");
                    return false;
                }
            }

            return true;
        }
        #endregion

        #region Copy
        void Btn_Copy_Click(object sender, EventArgs e)
        {
            try
            {
                IEBD EBDService = AppContainer.Instance[ServiceConst.EBDService] as IEBD;
                EBDService.CopyCR(this.AppID);
            }
            catch (Exception exp)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
            }
            Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM004 + "');</script>");

            // this.Btn_Copy.Visible = true;
            this.ctrlExport.Visible = true;
        }

        private DataTable InitializeDataTable(System.Data.DataTable dataTable)
        {
            DataRow dataRow;
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                dataRow = dataTable.Rows[i];
                dataRow[0] = 0;
            }
            return dataTable;
        }
        #endregion

        #region ButtonStatusSetting
        protected override void ButtonStatusSetting()
        {
            if (this.Access == null)
                return;

            this.Btn_Submit.Visible = this.Access.IsButtonVisible(EBDTransitionList.Submit);
            this.Btn_Save.Visible = this.Access.IsButtonVisible(EBDTransitionList.Save);
            this.Btn_Reply.Visible = this.Access.IsButtonVisible(EBDTransitionList.Reply);
            //this.Btn_Copy.Visible = this.Access.IsButtonVisible(EBDTransitionList.Copy);
            this.cvPrintCtrl.ProcessType = WorkflowTypes.EBD;
            this.cvPrintCtrl.Visible = this.Access.IsButtonVisible(EBDTransitionList.Copy);

            if (((this.Access.IsHideAllButton || this.Access.IsReadOnly) || (this.Step != WorkTransition.Draft
                && this.Step != WorkTransition.EBD_Request && this.Step != WorkTransition.EBD_ReRequest)) && !this.IsQuery)
                TR_title.Visible = false;

            if ((this.Access.IsReadOnly || this.IsView))
                isReadOnly = true;
        }
        #endregion

        #region Reply
        void Btn_Reply_Click(object sender, EventArgs e)
        {
            try
            {
                IEBD EBDService = AppContainer.Instance[ServiceConst.EBDService] as IEBD;
                IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                BEGDC.CC.Entity.Application App = EBDService.GetApplication(this.AppID);

                Application app = new Application();
                app.IsTransition = this.Transition;
                if (this.Application != null)
                {
                    app.Id = this.AppID;
                    app.AppNo = this.Application.AppNo;
                    app.IsTransition = this.Application.IsTransition;
                    app.NeedEVPApproval = this.Application.NeedEVPApproval;
                    app.DepartmentType = this.Application.DepartmentType;
                    app.DepartmentName = this.Application.DepartmentName;
                }

                CustomerInfo customer = new CustomerInfo();
                BusinessPurpose business = new BusinessPurpose();
                Topic topic = new Topic();
                SalesTeamInfo salesTeam = new SalesTeamInfo();
                LogisticalInfo logist = new LogisticalInfo();
                PaymentInfo payment = new PaymentInfo();
                IList customerAttendee = new ArrayList();
                IList alAttendee = new ArrayList();
                IList site = new ArrayList();
                IList cost = new ArrayList();
                IList contract = new ArrayList();
                //update Jovi
                DataTable HotelTable = null;
                DataTable CompanyCarTable = null;
                DataTable LeaseCarTable = null;

                EBDCtrl.BuildApplication(ref app, ref customer, ref business, ref topic, ref salesTeam, ref logist, ref payment, ref customerAttendee, ref alAttendee, ref site, ref cost, ref contract, ref HotelTable, ref CompanyCarTable, ref LeaseCarTable);
                //app.ApplicationType = WorkflowTypes.EBD;
                EBDService.SaveEZApplication(app, customer, business, topic, salesTeam, logist, payment, customerAttendee, alAttendee, site, cost, contract, HotelTable, CompanyCarTable, LeaseCarTable, false, true);

                EBDService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, tbComment.Text));

                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR001 + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');window.close();</script>");
            }
        }
        #endregion


        //private void fds()
        //{
        //    IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
        //    Employee CurrentRequestor = userService.GetUser(HttpContext.Current.User.Identity.Name);

        //    IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
        //    evService.Resume(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, ""));
        //}
    }
}
